/*
 * KeyboardHandler.java
 *
 * Created on July 30, 2007, 10:00 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.mar.bt.rd.server;

import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 *
 * @author helio.rodrigues
 */
public class KeyboardHandler {
    
    private Robot robot;
    
    /* special keys flags */
    private boolean shiftPressed = false;
    private boolean tabPressed = false;
    private boolean ctrlPressed = false;
    private boolean altPressed = false;
    private boolean altGraphPressed = false;
    
    /**
     * Creates a new instance of KeyboardHandler
     *
     * @param robot
     */
    public KeyboardHandler(Robot robot) {
        this.robot = robot;
    }
    
    /**
     *
     * @param keyCode
     */
    private void typeLowerCase(int keyCode) {
        robot.keyPress(keyCode);
        robot.keyRelease(keyCode);
    }
    
    /**
     *
     * @param keyCode
     */
    private void typeUpperCase(int keyCode) {
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(keyCode);
        robot.keyRelease(keyCode);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    }
    
    /**
     *
     * @param bytes
     */
    public void typeKeys(byte[] bytes) {
        String params = new String(bytes);
        
        for (int i = 0; i < params.length(); i++) {
            /* press special keys before typing */
            if (params.charAt(i) == '$') {
                for (int j = i + 1; j < params.length(); j++) {
                    if (params.charAt(j) == '$') {
                        String specialKey = params.substring(i + 1, j).trim();
                        int lastIndex = i;
                        i = j;
                        
                        if (specialKey.equals("ENTER")) {
                            robot.keyPress(KeyEvent.VK_ENTER);
                            robot.keyRelease(KeyEvent.VK_ENTER);
                        } else if (specialKey.equals("BACKSPACE")) {
                            robot.keyPress(KeyEvent.VK_BACK_SPACE);
                            robot.keyRelease(KeyEvent.VK_BACK_SPACE);
                        } else if (specialKey.equals("DELETE")) {
                            robot.keyPress(KeyEvent.VK_DELETE);
                            robot.keyRelease(KeyEvent.VK_DELETE);
                        } else if (specialKey.equals("HOME")) {
                            robot.keyPress(KeyEvent.VK_HOME);
                            robot.keyRelease(KeyEvent.VK_HOME);
                        } else if (specialKey.equals("INSERT")) {
                            robot.keyPress(KeyEvent.VK_INSERT);
                            robot.keyRelease(KeyEvent.VK_INSERT);
                        } else if (specialKey.equals("END")) {
                            robot.keyPress(KeyEvent.VK_END);
                            robot.keyRelease(KeyEvent.VK_END);
                        } else if (specialKey.equals("PAGEUP")) {
                            robot.keyPress(KeyEvent.VK_PAGE_UP);
                            robot.keyRelease(KeyEvent.VK_PAGE_UP);
                        } else if (specialKey.equals("PAGEDOWN")) {
                            robot.keyPress(KeyEvent.VK_PAGE_DOWN);
                            robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
                        } else if (specialKey.equals("ESC")) {
                            robot.keyPress(KeyEvent.VK_ESCAPE);
                            robot.keyRelease(KeyEvent.VK_ESCAPE);
                        } else if (specialKey.equals("NUMLOCK")) {
                            robot.keyPress(KeyEvent.VK_NUM_LOCK);
                            robot.keyRelease(KeyEvent.VK_NUM_LOCK);
                        } else if (specialKey.equals("F1")) {
                            robot.keyPress(KeyEvent.VK_F1);
                            robot.keyRelease(KeyEvent.VK_F1);
                        } else if (specialKey.equals("F2")) {
                            robot.keyPress(KeyEvent.VK_F2);
                            robot.keyRelease(KeyEvent.VK_F2);
                        } else if (specialKey.equals("F3")) {
                            robot.keyPress(KeyEvent.VK_F3);
                            robot.keyRelease(KeyEvent.VK_F3);
                        } else if (specialKey.equals("F4")) {
                            robot.keyPress(KeyEvent.VK_F4);
                            robot.keyRelease(KeyEvent.VK_F4);
                        } else if (specialKey.equals("F5")) {
                            robot.keyPress(KeyEvent.VK_F5);
                            robot.keyRelease(KeyEvent.VK_F5);
                        } else if (specialKey.equals("F6")) {
                            robot.keyPress(KeyEvent.VK_F6);
                            robot.keyRelease(KeyEvent.VK_F6);
                        } else if (specialKey.equals("F7")) {
                            robot.keyPress(KeyEvent.VK_F7);
                            robot.keyRelease(KeyEvent.VK_F7);
                        } else if (specialKey.equals("F8")) {
                            robot.keyPress(KeyEvent.VK_F8);
                            robot.keyRelease(KeyEvent.VK_F8);
                        } else if (specialKey.equals("F9")) {
                            robot.keyPress(KeyEvent.VK_F9);
                            robot.keyRelease(KeyEvent.VK_F9);
                        } else if (specialKey.equals("F10")) {
                            robot.keyPress(KeyEvent.VK_F10);
                            robot.keyRelease(KeyEvent.VK_F10);
                        } else if (specialKey.equals("F11")) {
                            robot.keyPress(KeyEvent.VK_F11);
                            robot.keyRelease(KeyEvent.VK_F11);
                        } else if (specialKey.equals("F12")) {
                            robot.keyPress(KeyEvent.VK_F12);
                            robot.keyRelease(KeyEvent.VK_F12);
                        } else if (specialKey.equals("SHIFT")) {
                            if (!shiftPressed) {
                                robot.keyPress(KeyEvent.VK_SHIFT);
                                shiftPressed = true;
                            } else {
                                robot.keyRelease(KeyEvent.VK_SHIFT);
                                shiftPressed = false;
                            }
                        } else if (specialKey.equals("TAB")) {
                            if (!tabPressed) {
                                robot.keyPress(KeyEvent.VK_TAB);
                                tabPressed = true;
                            } else {
                                robot.keyRelease(KeyEvent.VK_TAB);
                                tabPressed = false;
                            }
                        } else if (specialKey.equals("CTRL")) {
                            if (!ctrlPressed) {
                                robot.keyPress(KeyEvent.VK_CONTROL);
                                ctrlPressed = true;
                            } else {
                                robot.keyRelease(KeyEvent.VK_CONTROL);
                                ctrlPressed = false;
                            }
                        } else if (specialKey.equals("ALT")) {
                            if (!altPressed) {
                                robot.keyPress(KeyEvent.VK_ALT);
                                altPressed = true;
                            } else {
                                robot.keyRelease(KeyEvent.VK_ALT);
                                altPressed = false;
                            }
                        } else if (specialKey.equals("ALTGR")) {
                            if (!altGraphPressed) {
                                robot.keyPress(KeyEvent.VK_ALT_GRAPH);
                                altGraphPressed = true;
                            } else {
                                robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
                                altGraphPressed = false;
                            }
                        } else {
                            i = lastIndex;
                        }
                    }
                }
            }
            
            /* letters */
            if (params.charAt(i) == 'z') {
                typeLowerCase(KeyEvent.VK_Z);
            } else if (params.charAt(i) == 'Z') {
                typeUpperCase(KeyEvent.VK_Z);
            }
            
            if (params.charAt(i) == 'x') {
                typeLowerCase(KeyEvent.VK_X);
            } else if (params.charAt(i) == 'X') {
                typeUpperCase(KeyEvent.VK_X);
            }
            
            if (params.charAt(i) == 'c') {
                typeLowerCase(KeyEvent.VK_C);
            } else if (params.charAt(i) == 'C') {
                typeUpperCase(KeyEvent.VK_C);
            }
            
            if (params.charAt(i) == 'v') {
                typeLowerCase(KeyEvent.VK_V);
            } else if (params.charAt(i) == 'V') {
                typeUpperCase(KeyEvent.VK_V);
            }
            
            if (params.charAt(i) == 'b') {
                typeLowerCase(KeyEvent.VK_B);
            } else if (params.charAt(i) == 'B') {
                typeUpperCase(KeyEvent.VK_B);
            }
            
            if (params.charAt(i) == 'n') {
                typeLowerCase(KeyEvent.VK_N);
            } else if (params.charAt(i) == 'N') {
                typeUpperCase(KeyEvent.VK_N);
            }
            
            if (params.charAt(i) == 'm') {
                typeLowerCase(KeyEvent.VK_M);
            } else if (params.charAt(i) == 'M') {
                typeUpperCase(KeyEvent.VK_M);
            }
            
            if (params.charAt(i) == 'a') {
                typeLowerCase(KeyEvent.VK_A);
            } else if (params.charAt(i) == 'A') {
                typeUpperCase(KeyEvent.VK_A);
            }
            
            if (params.charAt(i) == 's') {
                typeLowerCase(KeyEvent.VK_S);
            } else if (params.charAt(i) == 'S') {
                typeUpperCase(KeyEvent.VK_S);
            }
            
            if (params.charAt(i) == 'd') {
                typeLowerCase(KeyEvent.VK_D);
            } else if (params.charAt(i) == 'D') {
                typeUpperCase(KeyEvent.VK_D);
            }
            
            if (params.charAt(i) == 'f') {
                typeLowerCase(KeyEvent.VK_F);
            } else if (params.charAt(i) == 'F') {
                typeUpperCase(KeyEvent.VK_F);
            }
            
            if (params.charAt(i) == 'g') {
                typeLowerCase(KeyEvent.VK_G);
            } else if (params.charAt(i) == 'G') {
                typeUpperCase(KeyEvent.VK_G);
            }
            
            if (params.charAt(i) == 'h') {
                typeLowerCase(KeyEvent.VK_H);
            } else if (params.charAt(i) == 'H') {
                typeUpperCase(KeyEvent.VK_H);
            }
            
            if (params.charAt(i) == 'j') {
                typeLowerCase(KeyEvent.VK_J);
            } else if (params.charAt(i) == 'J') {
                typeUpperCase(KeyEvent.VK_J);
            }
            
            if (params.charAt(i) == 'k') {
                typeLowerCase(KeyEvent.VK_K);
            } else if (params.charAt(i) == 'K') {
                typeUpperCase(KeyEvent.VK_K);
            }
            
            if (params.charAt(i) == 'l') {
                typeLowerCase(KeyEvent.VK_L);
            } else if (params.charAt(i) == 'L') {
                typeUpperCase(KeyEvent.VK_L);
            }
            
            if (params.charAt(i) == 'q') {
                typeLowerCase(KeyEvent.VK_Q);
            } else if (params.charAt(i) == 'Q') {
                typeUpperCase(KeyEvent.VK_Q);
            }
            
            if (params.charAt(i) == 'w') {
                typeLowerCase(KeyEvent.VK_W);
            } else if (params.charAt(i) == 'W') {
                typeUpperCase(KeyEvent.VK_W);
            }
            
            if (params.charAt(i) == 'e') {
                typeLowerCase(KeyEvent.VK_E);
            } else if (params.charAt(i) == 'E') {
                typeUpperCase(KeyEvent.VK_E);
            }
            
            if (params.charAt(i) == 'r') {
                typeLowerCase(KeyEvent.VK_R);
            } else if (params.charAt(i) == 'R') {
                typeUpperCase(KeyEvent.VK_R);
            }
            
            if (params.charAt(i) == 't') {
                typeLowerCase(KeyEvent.VK_T);
            } else if (params.charAt(i) == 'T') {
                typeUpperCase(KeyEvent.VK_T);
            }
            
            if (params.charAt(i) == 'y') {
                typeLowerCase(KeyEvent.VK_Y);
            } else if (params.charAt(i) == 'Y') {
                typeUpperCase(KeyEvent.VK_Y);
            }
            
            if (params.charAt(i) == 'u') {
                typeLowerCase(KeyEvent.VK_U);
            } else if (params.charAt(i) == 'U') {
                typeUpperCase(KeyEvent.VK_U);
            }
            
            if (params.charAt(i) == 'i') {
                typeLowerCase(KeyEvent.VK_I);
            } else if (params.charAt(i) == 'I') {
                typeUpperCase(KeyEvent.VK_I);
            }
            
            if (params.charAt(i) == 'o') {
                typeLowerCase(KeyEvent.VK_O);
            } else if (params.charAt(i) == 'O') {
                typeUpperCase(KeyEvent.VK_O);
            }
            
            if (params.charAt(i) == 'p') {
                typeLowerCase(KeyEvent.VK_P);
            } else if (params.charAt(i) == 'P') {
                typeUpperCase(KeyEvent.VK_P);
            }
            
            /* numbers */
            if (params.charAt(i) == '1') {
                typeLowerCase(KeyEvent.VK_1);
            } else if (params.charAt(i) == '!') {
                typeUpperCase(KeyEvent.VK_1);
            }
            
            if (params.charAt(i) == '2') {
                typeLowerCase(KeyEvent.VK_2);
            } else if (params.charAt(i) == '@') {
                typeUpperCase(KeyEvent.VK_2);
            }
            
            if (params.charAt(i) == '3') {
                typeLowerCase(KeyEvent.VK_3);
            } else if (params.charAt(i) == '#') {
                typeUpperCase(KeyEvent.VK_3);
            }
            
            if (params.charAt(i) == '4') {
                typeLowerCase(KeyEvent.VK_4);
            } else if (params.charAt(i) == '$') {
                typeUpperCase(KeyEvent.VK_4);
            }
            
            if (params.charAt(i) == '5') {
                typeLowerCase(KeyEvent.VK_5);
            } else if (params.charAt(i) == '%') {
                typeUpperCase(KeyEvent.VK_5);
            }
            
            if (params.charAt(i) == '6') {
                typeLowerCase(KeyEvent.VK_6);
            }
            
            if (params.charAt(i) == '7') {
                typeLowerCase(KeyEvent.VK_7);
            } else if (params.charAt(i) == '&') {
                typeUpperCase(KeyEvent.VK_7);
            }
            
            if (params.charAt(i) == '8') {
                typeLowerCase(KeyEvent.VK_8);
            } else if (params.charAt(i) == '*') {
                typeUpperCase(KeyEvent.VK_8);
            }
            
            if (params.charAt(i) == '9') {
                typeLowerCase(KeyEvent.VK_9);
            } else if (params.charAt(i) == '(') {
                typeUpperCase(KeyEvent.VK_9);
            }
            
            if (params.charAt(i) == '0') {
                typeLowerCase(KeyEvent.VK_0);
            } else if (params.charAt(i) == ')') {
                typeUpperCase(KeyEvent.VK_0);
            }
            
            /* symbols */
            if (params.charAt(i) == '\\') {
                typeLowerCase(KeyEvent.VK_BACK_SLASH);
            } else if (params.charAt(i) == '|') {
                typeUpperCase(KeyEvent.VK_BACK_SLASH);
            }
            
            if (params.charAt(i) == ',') {
                typeLowerCase(KeyEvent.VK_COMMA);
            } else if (params.charAt(i) == '<') {
                typeUpperCase(KeyEvent.VK_COMMA);
            }
            
            if (params.charAt(i) == '.') {
                typeLowerCase(KeyEvent.VK_PERIOD);
            } else if (params.charAt(i) == '>') {
                typeUpperCase(KeyEvent.VK_PERIOD);
            }
            
            if (params.charAt(i) == ';') {
                typeLowerCase(KeyEvent.VK_SEMICOLON);
            } else if (params.charAt(i) == ':') {
                typeUpperCase(KeyEvent.VK_SEMICOLON);
            }
            
            if (params.charAt(i) == '/') {
                typeLowerCase(KeyEvent.VK_SLASH);
            } else if (params.charAt(i) == '?') {
                typeUpperCase(KeyEvent.VK_SLASH);
            }
            
            if (params.charAt(i) == ']') {
                typeLowerCase(KeyEvent.VK_CLOSE_BRACKET);
            } else if (params.charAt(i) == '}') {
                typeUpperCase(KeyEvent.VK_CLOSE_BRACKET);
            }
            
            if (params.charAt(i) == '[') {
                typeLowerCase(KeyEvent.VK_OPEN_BRACKET);
            } else if (params.charAt(i) == '{') {
                typeUpperCase(KeyEvent.VK_OPEN_BRACKET);
            }
            
            if (params.charAt(i) == '\'') {
                typeLowerCase(KeyEvent.VK_QUOTE);
            } else if (params.charAt(i) == '\"') {
                typeUpperCase(KeyEvent.VK_QUOTE);
            }
            
            if (params.charAt(i) == '-') {
                typeLowerCase(KeyEvent.VK_MINUS);
            } else if (params.charAt(i) == '_') {
                typeUpperCase(KeyEvent.VK_MINUS);
            }
            
            if (params.charAt(i) == '=') {
                typeLowerCase(KeyEvent.VK_EQUALS);
            } else if (params.charAt(i) == '+') {
                typeUpperCase(KeyEvent.VK_EQUALS);
            }
            
            /* exceptions */
            if (params.charAt(i) == '~') {
                robot.keyPress(KeyEvent.VK_DEAD_TILDE);
                robot.keyRelease(KeyEvent.VK_DEAD_TILDE);
                robot.keyPress(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_SPACE);
            }
            
            if (params.charAt(i) == '^') {
                robot.keyPress(KeyEvent.VK_SHIFT);
                robot.keyPress(KeyEvent.VK_DEAD_TILDE);
                robot.keyRelease(KeyEvent.VK_DEAD_TILDE);
                robot.keyPress(KeyEvent.VK_SHIFT);
                robot.keyPress(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_SPACE);
            }
        }
    }
    
}
